package com.haois.mp.database.module.miniapp.service;

import com.haois.mp.database.module.miniapp.dao.MaUserFollowMapper;
import com.haois.mp.database.module.common.daomain.WxUser;
import com.haois.mp.database.util.jx.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Created by RuiHaoZ on 2018/2/25 16:53
 */
@Service
public class AppUserFollowService {
    @Autowired
    private MaUserFollowMapper maUserFollowMapper;


    public void subscribe(String openId) {
        WxUser wxUser = new WxUser();
        wxUser.setOpenId(openId);
        wxUser.setCreateTime(DateUtil.getYMdHm());
        maUserFollowMapper.addWxMpUser(wxUser);
    }


    public void updateUser(String openId, String userId, String remark) {
        WxUser wxUser = new WxUser();
        wxUser.setOpenId(openId);
        wxUser.setUserId(userId);
        wxUser.setRemark(remark);
        this.updateUser(wxUser);
    }

    public void updateUser(WxUser wxUser) {
        WxUser oldUser = maUserFollowMapper.getByOpenId(wxUser.getOpenId());
        if (oldUser == null) {
            this.subscribe(wxUser.getOpenId());
            oldUser = maUserFollowMapper.getByOpenId(wxUser.getOpenId());
        }
        oldUser.setUserId(wxUser.getUserId());
        oldUser.setRemark(StringUtils.isBlank(wxUser.getRemark()) ? "未知" : wxUser.getRemark());
        oldUser.setUpdateTime(DateUtil.getYMdHm());
        maUserFollowMapper.updateWxMpUser(oldUser);

    }
    public void syncWxMpUser(WxUser wxUser){
        maUserFollowMapper.syncWxMpUser(wxUser);
    }
}
